import { ApiResponse, ArticleRes, Channel } from '@/types/data'
import { HomeAction, RootThunkAction } from '@/types/store'
import axios from '@/utils/request'
import { getChannel, hasToken, setChannel } from '@/utils/storage'
/**
 *  获取用户频道
 * @returns
 */
export function getUserChannel(): RootThunkAction {
  return async (dispatch) => {
    if (hasToken()) {
      const res = await axios.get<ApiResponse<{ channels: Channel[] }>>(
        '/user/channels'
      )
      dispatch({
        type: 'home/saveUserChannel',
        payload: res.data.data.channels,
      })
    } else {
      //2.如果没有token
      const channels = getChannel()
      if (channels.length > 0) {
        // 3.如果本地有那么获取本地的数据
        dispatch({
          type: 'home/saveUserChannel',
          payload: channels,
        })
      } else {
        // 4. 如果本地没有数据那么就要发送请求获取数据 并且存储到本地
        const res = await axios.get<ApiResponse<{ channels: Channel[] }>>(
          '/user/channels'
        )
        dispatch({
          type: 'home/saveUserChannel',
          payload: res.data.data.channels,
        })
        setChannel(res.data.data.channels)
      }
    }
  }
}
/**
 *  获取所有频道列表
 * @returns
 */
export function getAllChannel(): RootThunkAction {
  return async (dispatch) => {
    // 1.如果有token
    const res = await axios.get<ApiResponse<{ channels: Channel[] }>>(
      '/channels'
    )
    dispatch({
      type: 'home/getAllChannel',
      payload: res.data.data.channels,
    })
  }
}
/**
 *  点击修改高亮
 * @param id
 * @returns
 */
export function changeActive(id: number): HomeAction {
  return {
    type: 'home/changeActive',
    id,
  }
}

/**
 * 根据id 点击删除相应的频道
 * @param id
 * @returns
 */
export function delChannel(id: number): RootThunkAction {
  return async (dispatch, getState) => {
    // 如果有token
    const { channels } = getState().home
    if (hasToken()) {
      await axios.delete('/user/channels', {
        data: {
          channels: [id],
        },
      })
    } else {
      //没有token
      const arr = channels.filter((item) => item.id !== id)
      // 存储到本地
      setChannel(arr)
    }
    dispatch(getUserChannel())
  }
}
/**
 *  添加频道
 * @param channel
 * @returns
 */
export function addChannel(channel: Channel): RootThunkAction {
  return async (dispatch, getState) => {
    const { channels } = getState().home
    if (hasToken()) {
      await axios.patch('/user/channels', {
        channels: [channel],
      })
    } else {
      // 如果没有token
      setChannel([...channels, channel])
    }
    dispatch(getUserChannel())
  }
}
/**
 * 获取文章列表
 * @param channel_id
 * @param timestamp
 * @returns
 */
export function getArticleList(
  channel_id: number,
  timestamp: number
): RootThunkAction {
  return async (dispatch) => {
    const res = await axios.get<ApiResponse<ArticleRes>>('/articles', {
      params: {
        channel_id,
        timestamp,
      },
    })
    // console.log(res.data.data)
    const { results, pre_timestamp } = res.data.data
    dispatch({
      type: 'home/saveChannelArticles',
      payload: {
        timestamp: pre_timestamp,
        articles: results,
        channel_id,
      },
    })
  }
}

export function getNewArticleList(
  channel_id: number,
  timestamp: number
): RootThunkAction {
  return async (dispatch) => {
    const res = await axios.get<ApiResponse<ArticleRes>>('/articles', {
      params: {
        channel_id,
        timestamp,
      },
    })
    // console.log(res.data.data)
    const { results, pre_timestamp } = res.data.data
    dispatch({
      type: 'home/saveNewChannelArticles',
      payload: {
        timestamp: pre_timestamp,
        articles: results,
        channel_id,
      },
    })
  }
}
